package com.yycx.module.bus.provider.handler.process.task;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.provider.service.BusDrugsPlanService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 日常消费审批任务列表
 *
 * @author zyf
 */
@Component
public class DrugsPlanTaskTodoListHandler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusDrugsPlanService busDrugsPlanService;

    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String companyId = OpenHelper.getCompanyId().toString();
        taskQuery.taskTenantId(companyId);
        taskQuery.processDefinitionKey("drugs_plan_" + companyId);
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                String drugsPlanId = taskResponse.getBusinessKey();
                if (FlymeUtils.isNotEmpty(drugsPlanId)) {
                    EntityMap busDrugsPlan = busDrugsPlanService.findByBusDrugsPlanId(drugsPlanId);
                    busDrugsPlan.put("assignee", taskResponse.getAssignee());
                    busDrugsPlan.put("endTime", taskResponse.getEndTime());
                    busDrugsPlan.put("taskId", taskResponse.getId());
                    list.add(busDrugsPlan);
                }
            }
        }
        return list;
    }


}
